package com.czy.security.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @作者:褚
 * @工程名:spring_security
 * @方法:SpringSecurityConfig
 * @创建时间:2022/4/12 10:18
 */
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private UserDetailsService userDetailsService;
    @Bean
    public PasswordEncoder getPasswordEncoder(){
        return NoOpPasswordEncoder.getInstance();
    }
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
       /* auth.inMemoryAuthentication().withUser("zhangsan").password("0000").roles("admin").and()
                .withUser("lisi").password("1111").roles("role");*/
        auth.userDetailsService(userDetailsService);
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/","menu.html").permitAll()
                .antMatchers("/admin/**").hasRole("admin_manage")
                .antMatchers("/role/**").hasRole("role_manage")
                .antMatchers("/**").authenticated();
        http.formLogin().loginPage("/login.html").loginProcessingUrl("/login")
                .permitAll().and().csrf().disable();
        http.exceptionHandling().accessDeniedPage("/403.html");
        http.logout().logoutUrl("/logout").clearAuthentication(true).invalidateHttpSession(true);

    }
}
